<template>
	<xn-form-container
		:title="formData.id ? '编辑题库表' : '增加题库表'"
		:width="1500"
		v-model:open="open"
		:destroy-on-close="true"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-form-item label="所属题库分类：" name="categoryId">
				<a-tree-select
					v-model:value="formData.categoryId"
					class="xn-wd"
					:dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
					placeholder="请选择所属题库分类"
					allow-clear
					tree-default-expand-all
					:tree-data="treeData"
					:field-names="{
						children: 'children',
						label: 'name',
						value: 'id'
					}"
					selectable="false"
					tree-line
				/>
			</a-form-item>
			<a-form-item label="题目：" name="questionTitle">
				<xn-editor v-model:value="formData.questionTitle" placeholder="请输入题目" />
			</a-form-item>
			<a-form-item label="类型：" name="questionType">
				<a-select v-model:value="formData.questionType" placeholder="请选择类型" :options="questionTypeOptions" />
			</a-form-item>
			<a-form-item :label="getAnswerShow()" name="answer">
				<!--    单选    -->
				<One
					:issueOption="formData.issueOption"
					:answer="formData.answer"
					@updateOneData="(val) => (formData.issueOption = val)"
					@updateAnswer="(val) => (formData.answer = val)"
					v-if="formData.questionType === 'one' || formData.questionType === 'judge'"
				/>
				<!--    多选    -->
				<Multi
					:answer="formData.answer"
					:issueOption="formData.issueOption"
					v-if="formData.questionType === 'multi'"
					@updateIssueOption="(val) => (formData.issueOption = val)"
					@updateAnswer="(val) => (formData.answer = val)"
				/>
				<Fill
					:answer="formData.answer"
					v-if="formData.questionType === 'fill'"
					@updateIssueOption="(val) => (formData.issueOption = val)"
					@updateAnswer="(val) => (formData.answer = val)"
				/>
				<Simple
					v-if="formData.questionType === 'simple'"
					:answer="formData.answer"
					@updateIssueOption="(val) => (formData.issueOption = val)"
					@updateAnswer="(val) => (formData.answer = val)"
				/>
			</a-form-item>
			<a-form-item label="解析：" name="analysis">
				<xn-editor v-model:value="formData.analysis" placeholder="请输入解析" />
			</a-form-item>
			<a-form-item label="难度：" name="difficulty">
				<a-select v-model:value="formData.difficulty" placeholder="请选择难度" :options="difficultyOptions" />
			</a-form-item>
			<a-form-item label="状态：" name="status">
				<a-radio-group v-model:value="formData.status" placeholder="请选择状态" :options="statusOptions" />
			</a-form-item>
			<a-form-item label="问题视频：" name="issueVideo">
				<xn-upload v-model:value="formData.issueVideo" />
			</a-form-item>
			<a-form-item label="解析视频：" name="analysisVideo">
				<xn-upload v-model:value="formData.analysisVideo" />
			</a-form-item>
			<a-form-item label="备注：" name="remark">
				<a-textarea v-model:value="formData.remark" placeholder="请输入备注" :auto-size="{ minRows: 3, maxRows: 5 }" />
			</a-form-item>
		</a-form>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button type="primary" @click="onSubmit" :loading="submitLoading">保存</a-button>
		</template>
	</xn-form-container>
</template>

<script setup name="tblQuestionForm">
	import tool from '@/utils/tool'
	import { cloneDeep } from 'lodash-es'
	import tblQuestionApi from '@/api/biz/tblQuestionApi'
	import tblCategoryApi from '@/api/biz/tblCategoryApi'
	import One from '@/views/biz/question/one.vue'
	import Multi from '@/views/biz/question/multi.vue'
	import Fill from '@/views/biz/question/fill.vue'
	import Simple from '@/views/biz/question/simple.vue'
	// 抽屉状态
	const open = ref(false)
	const emit = defineEmits({ successful: null })
	const formRef = ref()
	// 表单数据
	const formData = ref({
		status: '0',
		answer: ''
	})
	const submitLoading = ref(false)
	const questionTypeOptions = ref([])
	const statusOptions = ref([])
	const difficultyOptions = ref([])
	const treeData = ref([])
	// 打开抽屉
	const onOpen = (record) => {
		open.value = true
		if (record) {
			let recordData = cloneDeep(record)
			formData.value = Object.assign({}, recordData)
		}
		questionTypeOptions.value = tool.dictList('question_type')
		statusOptions.value = tool.dictList('status')
		difficultyOptions.value = tool.dictList('question_difficulty')
		tblCategoryApi.categoryTreeSelector({ type: 'library' }).then((res) => {
			treeData.value = res
		})
	}
	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		formData.value = {}
		open.value = false
	}
	// 默认要校验的
	const formRules = {
		categoryId: [{ required: true, message: '所属题库分类不能为空' }],
		questionType: [{ required: true, message: '类型不能为空' }],
		questionTitle: [{ required: true, message: '题目不能为空' }],
		answer: [{ required: true, message: '答案不能为空' }],
		status: [{ required: true, message: '状态不能为空' }],
		difficulty: [{ required: true, message: '难度不能为空' }]
	}
	// 验证并提交数据
	const onSubmit = () => {
		formRef.value
			.validate()
			.then(() => {
				submitLoading.value = true
				const formDataParam = cloneDeep(formData.value)
				tblQuestionApi
					.tblQuestionSubmitForm(formDataParam, formDataParam.id)
					.then(() => {
						onClose()
						emit('successful')
					})
					.finally(() => {
						submitLoading.value = false
					})
			})
			.catch(() => {})
	}

	const getAnswerShow = () => {
		if (
			formData.value.questionType === 'one' ||
			formData.value.questionType === 'multi' ||
			formData.value.questionType === 'judge'
		) {
			return '答案：【' + formData.value.answer + '】'
		}
		return '答案：'
	}
	// 抛出函数
	defineExpose({
		onOpen
	})
</script>
